﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLUNZ.Business;

namespace NLUNZ.UserControls
{
    public partial class DeliveryItemAdd : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                resultItem.Visible = true;
                updateInfo.Visible = false;
                CustomDataBind();
                CityDataBind();
            }
        }

        private void CityDataBind()
        {
            
            List<City> cities = Repository.GetCities();
            ddlCity.DataSource = cities;
            ddlCity.DataTextField = "Name";
            ddlCity.DataValueField = "ID";
            ddlCity.DataBind();
        }

        private void CustomDataBind()
        {
            Custom emptyCustom = new Custom() {ID = 0, Name = "选择海关"};

            List<Custom> customs = new List<Custom>();
            customs.Add(emptyCustom);
            customs.AddRange(Repository.GetAllCustom());
            DropDownListCustoms.DataSource = customs;
            DropDownListCustoms.DataTextField = "Name";
            DropDownListCustoms.DataValueField = "ID";
            DropDownListCustoms.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string deliveryNumber = txtDeliveryNumber.Text;
            if (Repository.DeliveryNumberExist(deliveryNumber))
            {
                updateInfo.InnerHtml = Utility.ErrorHtml("错误：运单号已经在数据库，更新该运单或者重新确认运单号。");
                updateInfo.Visible = true;
                return;
            }

            bool hasError = false;
            updateInfo.Visible = false;
            string customKey = DropDownListCustoms.SelectedValue;
            int customId = int.Parse(customKey);
            string customName = string.Empty;
            if(customId >= 0)
            {
                try
                {
                    customName = Repository.GetCustomByID(customId);
                    Delivery delivery = new Delivery() {DeliveryNum = txtDeliveryNumber.Text};
                    delivery.IsCheckedOut = CheckBoxCheckedOut.Checked;
                    delivery.ReceivedDateTime = Utility.SetKendoDateTime(pickupDate.Value);
                    delivery.InventoryDateTime = Utility.SetKendoDateTime(inventoryDate.Value);
                    delivery.AirportDateTime = Utility.SetKendoDateTime(arriveAirportDate.Value);
                    delivery.DeliveryDateTime = Utility.SetKendoDateTime(deliveryDate.Value);
                    delivery.DeliveryCity = ddlCity.SelectedItem.Text;
                    delivery.CustomDate = Utility.SetKendoDateTime(customClearDate.Value);
                    delivery.Recipient = txtRecipient.Text;
                    delivery.Custom = customName;
                    if (!Repository.InsertDeliveryRecordSuccess(delivery))
                        hasError = true;
                }
                catch (Exception)
                {
                    hasError = true;
                }

            }                
            

            if (hasError)
                updateInfo.InnerHtml = Utility.ErrorHtml("运单信息出错，请确认后再试！");
            else
            {
                updateInfo.InnerHtml = Utility.SuccessHtml(string.Format("运单 {0} 已经添加成功!", txtDeliveryNumber.Text));
            }
            updateInfo.Visible = true;
        }
    }
}